import 'package:flutter/material.dart';
import 'package:flutter_widgets_pc/config/kq_global.dart';
import 'package:flutter_widgets_pc/widgets/bottomSheet/sheets/kq_action_list_bottom_sheet.dart';
import 'package:flutter_widgets_pc/widgets/bottomSheet/sheets/kq_date_picker_bottom_sheet.dart';
import 'sheets/kq_bottom_sheet_view.dart';
import 'sheets/kq_date_range_picker_bottom_sheet.dart';

/// 通用底部弹窗
class KqBottomSheetManager {
  static showBottomSheet(
    String title, {
    required Widget child,
    double? childHeight,
    double? childMaxHeight,
    bool? isShowBottomBtn,
    CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.start,
    Function(int index)? tabIndexBlock,
    String? cancelStr,
    Color? cancelColor,
    Color? cancelBgColor,
    String? confirmStr,
    Color? confirmColor,
    Color? confirmBgColor,
    double? iconWidth,
    double? iconHeight,
    double? iconRight,
    VoidCallback? onDismiss,
  }) {
    showModalBottomSheet(
      context: KqGlobal.globalContext,
      isScrollControlled: true,
      isDismissible: true,
      constraints: const BoxConstraints(),
      builder: (context) {
        return KqBottomSheetView(
          childHeight: childHeight,
          childMaxHeight: childMaxHeight,
          iconHeight: iconHeight,
          iconWidth: iconWidth,
          iconRight: iconRight,
          title: title,
          isBottomBtn: isShowBottomBtn ?? true,
          tabIndexBlock: tabIndexBlock,
          cancelStr: cancelStr,
          cancelColor: cancelColor,
          cancelBgColor: cancelBgColor,
          confirmStr: confirmStr,
          confirmColor: confirmColor,
          confirmBgColor: confirmBgColor,
          crossAxisAlignment: crossAxisAlignment,
          child: child,
        );
      },
    ).then((value) {
      onDismiss?.call();
    });
  }

  /// 日期选择
  static showDatePicker({
    String? title,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialDateTime,
    String? leftBtnText,
    String? rightBtnText,
    Function(DateTime dateTime, List<int> selectedIndex)? onConfirm,
    Function(DateTime dateTime, List<int> selectedIndex)? onChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    return showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDatePickerBottomSheet(
          initialDateTime: initialDateTime ?? DateTime.now(),
          pickerMode: KqDateTimePickerMode.date,
          minDateTime: minDateTime,
          maxDateTime: maxDateTime,
          leftBtnText: leftBtnText,
          rightBtnText: rightBtnText,
          pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
          dateFormat: 'yyyy年,MM月,dd日',
          onConfirm: onConfirm,
          onCancel: onCancel,
          onChange: onChange),
    ).then((value) => {onDismiss?.call()});
  }

  /// 时间选择对话框
  static showTimePicker({
    String? title,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialDateTime,
    String? leftBtnText,
    String? rightBtnText,
    Function(DateTime dateTime, List<int> selectedIndex)? onConfirm,
    Function(DateTime dateTime, List<int> selectedIndex)? onChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    return showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDatePickerBottomSheet(
          initialDateTime: initialDateTime ?? DateTime.now(),
          pickerMode: KqDateTimePickerMode.time,
          minDateTime: minDateTime,
          maxDateTime: maxDateTime,
          leftBtnText: leftBtnText,
          rightBtnText: rightBtnText,
          pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
          dateFormat: 'HH时:mm分:ss秒',
          onConfirm: onConfirm,
          onCancel: onCancel,
          onChange: onChange),
    ).then((value) => {onDismiss?.call()});
  }

  /// 日期时间选择底部弹窗
  static showDateTimePicker({
    String? title,
    bool withSecond = true,
    bool withMinute = true,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialDateTime,
    String? leftBtnText,
    String? rightBtnText,
    Function(DateTime dateTime, List<int> selectedIndex)? onConfirm,
    Function(DateTime dateTime, List<int> selectedIndex)? onChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDatePickerBottomSheet(
          initialDateTime: initialDateTime ?? DateTime.now(),
          pickerMode: KqDateTimePickerMode.datetime,
          minDateTime: minDateTime,
          maxDateTime: maxDateTime,
          leftBtnText: leftBtnText,
          rightBtnText: rightBtnText,
          pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
          dateFormat:
              'yyyy年,MM月,dd日,HH时${withMinute ? ':mm分' : ''}${withSecond ? ':ss秒' : ''}',
          onConfirm: onConfirm,
          onCancel: onCancel,
          onChange: onChange),
    ).then((value) => {onDismiss?.call()});
  }

  /// 日期范围选择
  static showDateRangePicker({
    String? title,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialStartDateTime,
    DateTime? initialEndDateTime,
    String? hintText,
    String? leftBtnText,
    String? rightBtnText,
    double? resultFontSize,
    bool isSelectEndTime = false,
    List<Widget>? customHeaders,
    required bool Function(DateTime startDatetime, DateTime endDatetime)
        onConfirm,
    Function(DateTime dateTime)? onStartTimeChange,
    Function(DateTime dateTime)? onEndTimeChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    return showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDateRangePickerBottomSheet(
        initialStartDateTime: initialStartDateTime ?? DateTime.now(),
        initialEndDateTime: initialEndDateTime ?? DateTime.now(),
        pickerMode: KqDateTimePickerMode.date,
        minDateTime: minDateTime,
        maxDateTime: maxDateTime,
        leftBtnText: leftBtnText,
        rightBtnText: rightBtnText,
        hintText: hintText,
        customHeaders: customHeaders,
        resultFontSize: resultFontSize,
        isSelectEndTime: isSelectEndTime,
        pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
        pickerFormat: 'yyyy年,MM月,dd日',
        dateFormat: 'yyyy-MM-dd',
        onConfirm: onConfirm,
        onCancel: onCancel,
        onStartTimeChange: onStartTimeChange,
        onEndTimeChange: onEndTimeChange,
      ),
    ).then((value) => {onDismiss?.call()});
  }

  /// 时间选择对话框
  static showTimeRangePicker({
    String? title,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialStartDateTime,
    DateTime? initialEndDateTime,
    String? dateFormat,
    String? hintText,
    String? leftBtnText,
    String? rightBtnText,
    double? resultFontSize,
    bool isSelectEndTime = false,
    List<Widget>? customHeaders,
    required bool Function(DateTime startDatetime, DateTime endDatetime)
        onConfirm,
    Function(DateTime dateTime)? onStartTimeChange,
    Function(DateTime dateTime)? onEndTimeChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    return showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDateRangePickerBottomSheet(
        initialStartDateTime: initialStartDateTime ?? DateTime.now(),
        initialEndDateTime: initialEndDateTime ?? DateTime.now(),
        pickerMode: KqDateTimePickerMode.time,
        minDateTime: minDateTime,
        maxDateTime: maxDateTime,
        leftBtnText: leftBtnText,
        rightBtnText: rightBtnText,
        resultFontSize: resultFontSize,
        hintText: hintText,
        customHeaders: customHeaders,
        isSelectEndTime: isSelectEndTime,
        pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
        pickerFormat: 'HH时:mm分:ss秒',
        dateFormat: dateFormat ?? 'HH:mm:ss',
        onConfirm: onConfirm,
        onCancel: onCancel,
        onStartTimeChange: onStartTimeChange,
        onEndTimeChange: onEndTimeChange,
      ),
    ).then((value) => {onDismiss?.call()});
  }

  /// 日期时间选择底部弹窗
  static showDateTimeRangePicker({
    String? title,
    bool withSecond = true,
    bool withMinute = true,
    DateTime? minDateTime,
    DateTime? maxDateTime,
    DateTime? initialStartDateTime,
    DateTime? initialEndDateTime,
    String? leftBtnText,
    String? rightBtnText,
    String? dateFormat,
    String? hintText,
    double? resultFontSize,
    bool isSelectEndTime = false,
    List<Widget>? customHeaders,
    required bool Function(DateTime startDatetime, DateTime endDatetime)
        onConfirm,
    Function(DateTime dateTime)? onStartTimeChange,
    Function(DateTime dateTime)? onEndTimeChange,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqDateRangePickerBottomSheet(
        initialStartDateTime: initialStartDateTime ?? DateTime.now(),
        initialEndDateTime: initialEndDateTime ?? DateTime.now(),
        pickerMode: KqDateTimePickerMode.datetime,
        minDateTime: minDateTime,
        maxDateTime: maxDateTime,
        hintText: hintText,
        leftBtnText: leftBtnText,
        rightBtnText: rightBtnText,
        customHeaders: customHeaders,
        resultFontSize: resultFontSize,
        isSelectEndTime: isSelectEndTime,
        pickerTitleConfig: KqPickerTitleConfig(titleContent: title),
        pickerFormat:
            'yyyy年,MM月,dd日,HH时${withMinute ? ':mm分' : ''}${withSecond ? ':ss秒' : ''}',
        dateFormat: dateFormat ??
            (!withMinute
                ? 'yyyy-MM-dd HH'
                : (withSecond ? 'yyyy-MM-dd HH:mm:ss' : 'yyyy-MM-dd HH:mm')),
        onConfirm: onConfirm,
        onCancel: onCancel,
        onStartTimeChange: onStartTimeChange,
        onEndTimeChange: onEndTimeChange,
      ),
    ).then((value) => {onDismiss?.call()});
  }

  /// 显示底部列表弹出菜单
  static showActionList(
    List<KqActionListData> items, {
    String? title,
    Alignment? alignment,
    bool? showCancel,
    required Function(int index, KqActionListData item)? callback,
    Function()? onDismiss,
    Function()? onCancel,
  }) {
    showModalBottomSheet(
      context: KqGlobal.globalContext,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      constraints: const BoxConstraints(),
      builder: (context) => KqActionListBottomSheet(
        items: items,
        title: title,
        alignment: alignment,
        showCancel: showCancel ?? true,
        callback: callback,
        onCancel: onCancel,
      ),
    ).then((value) => {onDismiss?.call()});
  }
}

class KqPickerTitleConfig {
  /// DateTimePicker theme.
  ///
  /// [cancel] Custom cancel widget.
  /// [confirm] Custom confirm widget.
  /// [title] Custom title widget. If specify a title widget, the cancel and confirm widgets will not display. Must set [titleHeight] value for custom title widget.
  /// [showTitle] Whether display title widget or not. If set false, the default cancel and confirm widgets will not display, but the custom title widget will display if had specified one custom title widget.
  /// [titleContent] Title content
  const KqPickerTitleConfig({
    this.cancel,
    this.confirm,
    this.title,
    this.showTitle = true,
    this.titleContent,
  });

  static const KqPickerTitleConfig Default = const KqPickerTitleConfig();

  /// Custom cancel [Widget].
  final Widget? cancel;

  /// Custom confirm [Widget].
  final Widget? confirm;

  /// Custom title [Widget]. If specify a title widget, the cancel and confirm widgets will not display.
  final Widget? title;

  /// Whether display title widget or not. If set false, the default cancel and confirm widgets will not display, but the custom title widget will display if had specified one custom title widget.
  final bool showTitle;

  final String? titleContent;

  KqPickerTitleConfig copyWith({
    Widget? cancel,
    Widget? confirm,
    Widget? title,
    bool? showTitle,
    String? titleContent,
  }) {
    return KqPickerTitleConfig(
      cancel: cancel ?? this.cancel,
      confirm: confirm ?? this.confirm,
      title: title ?? this.title,
      showTitle: showTitle ?? this.showTitle,
      titleContent: titleContent ?? this.titleContent,
    );
  }
}
